﻿using System;
using System.Configuration.Provider;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web.Security;
using Mal.Web.Security.DataModel;
using Mal.Web.Security.Provider;

namespace Mal.Web.Security
{
    public class Permissions
    {
        ///<summary>Gets or sets the name of the application 
        ///to store and retrieve permission information for. 
        ///</summary>
        public static string ApplicationName
        {
            get
            {
                if (Provider == null)
                {
                    ProviderException exp = new ProviderException("Provider must not be null");
                    throw exp;
                }
                return Provider.ApplicationName;
            }

            set
            {
                if (Provider == null)
                {
                    ProviderException exp = new ProviderException("Provider must not be null");
                    throw exp;
                }
                Provider.ApplicationName = value;
            }
        }

        ///<summary>Gets or set the permission provider for the application.  </summary>
        public static PermissionProvider Provider { get; set; }

        private static void CheckStaticProperties()
        {
            if (Provider == null)
            {
                ProviderException exp = new ProviderException("Provider must not be null");
                throw exp;
            }
        }

        /// <summary>
        /// Gets a value indicating whether the currently logged-on user 
        /// has in the specified permission. 
        /// </summary>
        /// <param name="permissionName">The name of the permission to search in.</param>
        /// <returns>true if the currently logged-on user has in the specified permission; otherwise, false. </returns>
        public static bool UserHasPermission(string permissionName)
        {
            CheckStaticProperties();
            if (Thread.CurrentPrincipal != null)
                return UserHasPermission(Thread.CurrentPrincipal.Identity.Name, permissionName);
            else
                return false;
        }

        /// <summary>
        /// Gets a value indicating whether the specified user has the specified permission. 
        /// </summary>
        /// <param name="userName">The name of the user to search for. </param>
        /// <param name="permissionName">The name of the permission to search in.</param>
        /// <returns>true if the currently logged-on user has in the specified permission; otherwise, false. </returns>
        public static bool UserHasPermission(string userName, string permissionName)
        {
            CheckStaticProperties();

            if (string.IsNullOrEmpty(userName))
            {
                ArgumentNullException aneu = new ArgumentNullException("userName");
                throw aneu;
            }

            if (string.IsNullOrEmpty(permissionName))
            {
                ArgumentNullException aneu = new ArgumentNullException("permissionName");
                throw aneu;
            }
            return Provider.UserHasPermission(userName, permissionName);
        }

        public static bool Authenticate(string userName, string password)
        {
            CheckStaticProperties();
            if (string.IsNullOrEmpty(userName))
            {
                ArgumentNullException aneu = new ArgumentNullException("userName");
                throw aneu;
            }

            if (string.IsNullOrEmpty(password))
            {
                ArgumentNullException aneu = new ArgumentNullException("password");
                throw aneu;
            }

            return Provider.Authenticate(userName, password);
        }

        /// <summary>
        /// Gets the UserId
        /// </summary>
        /// <returns> </returns>
        public static Guid? GetUserId()
        {
            CheckStaticProperties();
            if (Thread.CurrentPrincipal != null)
                return GetUserId(Thread.CurrentPrincipal.Identity.Name);
            else
                return null;
        }

        /// <summary>
        /// Gets the UserId
        /// </summary>
        /// <param name="userName">The name of the user to search for. </param>
        /// <returns> </returns>
        public static Guid? GetUserId(string userName)
        {
            CheckStaticProperties();

            if (string.IsNullOrEmpty(userName))
            {
                ArgumentNullException aneu = new ArgumentNullException("userName");
                throw aneu;
            }
            return Provider.GetUserId(userName);
        }
    }
}
